package com.himiotech.core.project.web.base;

import com.himiotech.core.framework.helper.utils.ShiroUtils;
import com.himiotech.core.project.model.entity.user.User;
import com.himiotech.core.project.service.IConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wuwenbin
 */
@ControllerAdvice(basePackages = "com.himiotech")
public class AttributeControllerAdvice {

    @Autowired
    private IConfigService configService;

    @ModelAttribute("globalAttr")
    public Map<String, Object> globalAttr(ModelMap mmp) {
        Map<String, Object> attr = new HashMap<>(4);
        String theme = configService.selectConfigByKey("sys.index.sideTheme");
        attr.put("sideTheme", theme);
        if (ShiroUtils.getSubject() != null) {
            User sysUser = ShiroUtils.getSysUser();
            if (sysUser != null) {
                Map<String, Object> sessionUser = new HashMap<>(3);
                sessionUser.put("userId", sysUser.getUserId());
                sessionUser.put("userName", sysUser.getUserName());
                sessionUser.put("loginName", sysUser.getLoginName());
                attr.put("sessionUser", sessionUser);
            }
        }
        mmp.put("globalAttr", attr);
        return attr;
    }
}
